<template>
  <van-image width="100%" src="https://img.yzcdn.cn/vant/cat.jpeg" />
  <span class="register" @click="register">没有账号，请注册</span>
  <van-form @submit="onSubmit">
    <van-field
        v-model="phone"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
    />
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        登录
      </van-button>
    </div>
  </van-form>
</template>

<script>
import {login} from "@/api/user";
import {Toast} from "vant";

export default {
  name: "Login",
  data(){
    return {
      phone:"",
      password:""
    }
  },
  methods:{
    onSubmit(){
      var data = {phone:this.phone,password:this.password}
      //console.log(data)
      login(data).then((res)=>{
        if(res.code==200){
          this.$store.commit("SET_TOKEN","Bearer "+res.data.token.token)
          this.$store.commit("SET_USER",res.data)
          this.$router.push('my')
        }else {
          Toast.fail(res.msg)
        }
      })
    },
    register(){
      this.$router.push("/register")
    }
  }
}
</script>

<style scoped>
  .register{
    font-size: 14px;
    position: absolute;
    top: 10px;
    right: 10px;
    color: #ffffff;
  }
</style>
